package com.testyourshell.data;

import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class TestYourShellUserDetails implements UserDetails {

	private static final long serialVersionUID = 1L;

	private String username;
	private String password;
	private Collection<GrantedAuthority> authorities;
	private boolean accountNonExpired;
	private boolean accountNonLocked;
	private boolean credentialsNonExpired;
	private boolean enabled;

	// *** Getters
	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public boolean isAccountNonExpired() {
		return accountNonExpired;
	}

	@Override
	public boolean isAccountNonLocked() {
		return accountNonLocked;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return credentialsNonExpired;
	}

	@Override
	public boolean isEnabled() {
		return enabled;
	}

	// *** Setters
	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	public void setAccountNonExpired(boolean accountNonExpired) {
		this.accountNonExpired = accountNonExpired;
	}

	public void setAccountNonLocked(boolean accountNonLocked) {
		this.accountNonLocked = accountNonLocked;
	}

	public void setCredentialsNonExpired(boolean credentialsNonExpired) {
		this.credentialsNonExpired = credentialsNonExpired;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	@Override
	public boolean equals(Object o) {
		if (o == null || !(o instanceof TestYourShellUserDetails))
			return false;
		TestYourShellUserDetails that = (TestYourShellUserDetails) o;
		return this.username.equals(that.username)
				&& this.password.equals(that.password)
				&& this.accountNonExpired == that.accountNonExpired
				&& this.accountNonLocked == that.accountNonLocked
				&& this.credentialsNonExpired == that.credentialsNonExpired
				&& this.enabled == that.enabled
				&& this.authorities == that.authorities;
	}

	@Override
	public int hashCode() {
		int result = username != null ? username.hashCode() : 0;

		result = 37 * result + (password == null ? password.hashCode() : 0);
		result = 37 * result + (accountNonExpired ? 0 : 1);
		result = 37 * result + (accountNonLocked ? 0 : 1);
		result = 37 * result + (credentialsNonExpired ? 0 : 1);
		result = 37 * result + (enabled ? 0 : 1);

		int authResult = 1;
		for (GrantedAuthority auth : authorities) {
			authResult = 37 * authResult + auth.hashCode();
		}

		return 37 * result + authResult;
	}
}
